<?php 

class RecipesController extends AppController{
	
	public $scaffold;
	
	
	public function admin_add(){

		
		
		$categories = $this->_get_treelist_categories();
		
		$templates = $this->Recipe->RecipeTemplate->find('all', array(	'recursive' => -1,
																		'fields' => array('RecipeTemplate.id', 'RecipeTemplate.title')
																	)
														);
		
		$mains = $this->Recipe->RecipeMain->find('all', array (	'recursive' => -1,
																'fields' => array(	'RecipeMain.id', 'RecipeMain.title')
																)
												);

		debug($categories);
		
	}
	
	
	
	/***
	
	public function demo_save(){
		
		$demo_recipe = array(	'Recipe' => array('id' => 4),
								'RecipeTemplate' => array('id' => 2),
								'RecipeCategory' => array( 'RecipeCategory' => array(1, 7)),
								'RecipeMain' => array('RecipeMain'=> array(1,3))
								);
		
		debug($demo_recipe);
				
		$this->Recipe->saveAll($demo_recipe);
		
		
	}
	
	****/
	
	private function _get_treelist_categories($parent_category=null){
		
		
		if(!$parent_category){
			
			$parent_category = $this->Recipe->RecipeCategory->find('first', array(	'conditions' => array('RecipeCategory.parent_id' => null),
																					'recursive' => -1,
																					'fields' => array('RecipeCategory.id', 'RecipeCategory.title')
																					)
																	);
		}
		
		
		$all_children = $this->Recipe->RecipeCategory->find('all', array(	'conditions' => array('RecipeCategory.parent_id' => $parent_category['RecipeCategory']['id']),
																			'recursive' => -1,
																			'fields' => array('RecipeCategory.id', 'RecipeCategory.title')
																			)
															);
		
		if($all_children){
		
			for($i=0; $i< count($all_children); $i++){
				
				
				$all_children[$i] = $this->_get_treelist_categories($all_children[$i]);
			}
			
		}
		
		$parent_category['RecipeCategory']['Children'] = $all_children;
		
		return $parent_category;	
		
	}
	

}



?>